home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 4 / QRZ Ham Radio Callsign Database - Volume 4.iso / files / satelite / msat09.tgz / PHS.C < prev    next >
Text File  |  1994-09-17  |  4KB  |  201 lines

  1. /*
  2.  *   Copyright 1992, 1993, 1994 John Melton (G0ORX/N6LYT)
  3.  *              All Rights Reserved
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 1, or (at your option)
  8.  *   any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20.  
  21. /*
  22.     phs.c
  23.  
  24.     Wrapper to allow non PACSAT aware programs to be used with downloaded.
  25.  
  26.     John Melton
  27.     G0ORX, N6LYT
  28.  
  29.     4 Charlwoods Close
  30.     Copthorne
  31.     West Sussex
  32.     RH10 3QZ
  33.     England
  34.  
  35.     INTERNET:    g0orx@amsat.org
  36.             n6lyt@amsat.org
  37.             john@images.demon.co.uk
  38.             J.D.Melton@slh0613.icl.wins.co.uk
  39.  
  40.     History:
  41.  
  42.     0.1    Initial version.             G4KLX
  43. */
  44.  
  45. #define VERSION_STRING "(version 0.1 by g4klx)"
  46.  
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <unistd.h>
  50. #include <sys/types.h>
  51. #include <sys/wait.h>
  52. #include <sys/stat.h>
  53. #include <fcntl.h>
  54. #include <dirent.h>
  55. #include <signal.h>
  56. #include <ctype.h>
  57. #include <time.h>
  58.  
  59. #include <X11/Intrinsic.h>
  60.  
  61. #include "header.h"
  62.  
  63. int main(int argc, char **argv)
  64. {
  65.     FILE *fpin, *fpout;
  66.     char fileName[20], compFile[20], *pBuffer;
  67.     char command[50];
  68.     time_t time_now;
  69.     int i, nBytes, headerSize;
  70.     int pid, status;
  71.     HEADER *pHeader;
  72.     char *args[10];
  73.  
  74.     if (argc < 3)
  75.     {
  76.         fprintf(stderr, "usage: phs <infile> <program> args ...\n");
  77.         return(1);
  78.     }
  79.     
  80.     if ((fpin = fopen(argv[1], "r")) == NULL)
  81.     {
  82.         perror(argv[1]);
  83.         return(1);
  84.     }
  85.  
  86.     pBuffer = XtMalloc(1024);
  87.  
  88.     /* read in the header */
  89.     nBytes = fread(pBuffer, 1, 1024, fpin);
  90.  
  91.     /* extracting the header */
  92.     pHeader = ExtractHeader(pBuffer, nBytes, &headerSize);
  93.  
  94.     time(&time_now);
  95.  
  96.     switch (pHeader->fileType)
  97.     {
  98.     case 14:
  99.         sprintf(fileName, "%x.gif", (int)time_now);
  100.         break;
  101.     case 15:
  102.         sprintf(fileName, "%x.pcx", (int)time_now);
  103.         break;
  104.     case 16:
  105.         sprintf(fileName, "%x.jpg", (int)time_now);
  106.         break;
  107.     case 221:
  108.         sprintf(fileName, "%x.eis", (int)time_now);
  109.         break;
  110.     default:
  111.         sprintf(fileName, "%x.txt", (int)time_now);
  112.         break;
  113.     }
  114.  
  115.     if (pHeader->compression != 0 && pHeader->compression != 255)
  116.     {
  117.         sprintf(compFile, "%x.comp", (int)time_now);
  118.     
  119.         fseek(fpin, pHeader->bodyOffset, SEEK_SET);
  120.  
  121.         if ((fpout = fopen(compFile, "w")) == NULL)
  122.         {
  123.             perror(compFile);
  124.             return(1);
  125.         }
  126.  
  127.         while ((nBytes = fread(pBuffer, 1, 1024, fpin)) > 0)
  128.             fwrite(pBuffer, 1, nBytes, fpout);
  129.  
  130.         fclose(fpin);
  131.         fclose(fpout);
  132.     
  133.         switch (pHeader->compression)
  134.         {
  135.         case 2:
  136.             sprintf(command, "unzip -p %s > %s", compFile, fileName);
  137.             break;
  138.         case 3:
  139.             sprintf(command, "lha -pq %s > %s", compFile, fileName);
  140.             break;
  141.         default:
  142.             break;
  143.         }
  144.  
  145.         status = system(command);
  146.  
  147.         unlink(compFile);
  148.  
  149.         if (status == 127 || status < 0)
  150.         {
  151.             perror(command);
  152.             return(1);
  153.         }
  154.     }
  155.     else
  156.     {
  157.         if ((fpout = fopen(fileName, "w")) == NULL)
  158.         {
  159.             perror(fileName);
  160.             return(1);
  161.         }
  162.  
  163.         fseek(fpin, pHeader->bodyOffset, SEEK_SET);
  164.     
  165.         while ((nBytes = fread(pBuffer, 1, 1024, fpin)) > 0)
  166.             fwrite(pBuffer, 1, nBytes, fpout);
  167.         
  168.         fclose(fpout);
  169.         fclose(fpin);
  170.     }
  171.  
  172.     XtFree((char *)pHeader);
  173.     XtFree(pBuffer);
  174.  
  175.     for (i = 0; i < argc - 2; i++)
  176.         args[i] = argv[i + 2];
  177.         
  178.     args[i++] = fileName;
  179.     args[i++] = NULL;
  180.  
  181.     if ((pid = fork()) == 0)
  182.     {
  183.         /* the child process */
  184.         execvp(args[0], args);
  185.     }
  186.     else if (pid > 0)
  187.     {
  188.         /* success */
  189.         wait(&status);
  190.     }
  191.     else
  192.     {
  193.         return(1);
  194.     }
  195.  
  196.     unlink(fileName);
  197.     
  198.     return(0);
  199. }
  200.  
  201.